home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 045 (1988-02-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 045 (1988-02-15)(Ossowski, Stefan)(DE)(PD).adf / Iff2Pcs / Source / smalliff.c < prev    next >
C/C++ Source or Header  |  1988-01-20  |  3KB  |  118 lines

  1. #include <functions.h>
  2. #include <intuition/intuition.h> /* Nice because it includes everything else */
  3. #include <libraries/dos.h>
  4. #include "smalliff.h"
  5.  
  6. /* Ali T. Ozer, Oct, Nov 1987 */
  7.  
  8. /* This code to read IFF pictures originally from D. John Hodgson's "view" 
  9. ** program. LIMITATIONS : no masking, CATS/LISTS/PROPS. CAMG chunks 
  10. ** are supported.
  11. */
  12.  
  13. /* Original Copyright: 
  14. ** VIEW.C - tiny ILBM viewer; (c) 1986 DJH
  15. */
  16.  
  17. #define CRead(f,src,sz) if (Read (f, src, sz) == -1L) return (0);
  18.  
  19. int ReadILBM (fp, pic)
  20. struct FileHandle *fp;    /* An already "Open"ed AmigaDOS file handle */
  21. PicInfoStruct     *pic;
  22. {
  23.   unsigned char  cmap[MAXCOLORS][3];
  24.   ChunkHeader    header;
  25.   ID             id;
  26.   int            cnt;
  27.  
  28.   CRead(fp, &header, (long)sizeof(ChunkHeader));
  29.   if (header.ckID != ID_FORM) return (0);  /* Not IFF */
  30.  
  31.   CRead(fp, &id, (long)sizeof(id));
  32.   if (id != ID_ILBM) return (0);           /* Not ILBM */
  33.  
  34.   while (1) {
  35.  
  36.     CRead(fp, &header, (long)sizeof(header));
  37.  
  38.     if (header.ckID == ID_BODY) break;
  39.  
  40.     switch(header.ckID) {
  41.       case ID_BMHD: CRead(fp, &(pic->bmhd), (long) sizeof(BitMapHeader)); break;
  42.       case ID_CMAP: CRead(fp, &(cmap[0][0]), header.ckSize);
  43.                     pic->colorcount = (UBYTE)(header.ckSize/3); break;
  44.       case ID_CAMG: CRead(fp, &(pic->viewmodes), header.ckSize); break;
  45.       default:      Seek(fp, WordAlign(header.ckSize), OFFSET_CURRENT); break;
  46.     }
  47.   }
  48.  
  49.   pic->rawsourcesize  = header.ckSize;
  50.   if ((pic->rawsource = AllocMem (header.ckSize, NULL)) == NULL)
  51.      return (0);   /* Not enough memory */
  52.  
  53.   CRead(fp, pic->rawsource, header.ckSize);
  54.  
  55.   /* make some forced assumptions if CAMG chunk unavailable */
  56.  
  57.   if (pic->viewmodes == NULL) {
  58.     if (pic->bmhd.w > MAXWIDTH)  pic->viewmodes |= HIRES;
  59.     if (pic->bmhd.h > MAXHEIGHT) pic->viewmodes |= LACE;
  60.   }
  61.  
  62.   for (cnt = 0; cnt < pic->colorcount; cnt++) {
  63.     pic->colortable[cnt] = 
  64.        (((UWORD)cmap[cnt][0]) << 4L) | 
  65.        (((UWORD)cmap[cnt][1]))       |
  66.        (((UWORD)cmap[cnt][2]) >> 4L);
  67.   };
  68.       
  69.   return (1);
  70. }      
  71.  
  72.  
  73.  
  74. Expand(bm, bmhd, sourcebuf) /* Fast line decompress/deinterleave */
  75. struct BitMap *bm;
  76. BitMapHeader  *bmhd;
  77. register char *sourcebuf;
  78. {
  79.   
  80.   register char n,*destbuf; /* in order of preferred allocation */
  81.   register short plane,rowbytes;
  82.   short    linelen,i,curloc;
  83.  
  84.   linelen=bmhd->w/8;
  85.   curloc = 0;
  86.  
  87.   for (i=0;i<bmhd->h;i++) { /* process n lines/screen */
  88.     for (plane=0;plane<bmhd->nPlanes;plane++) { /* process n planes/line */
  89.       destbuf=(char *)(bm->Planes[plane])+curloc;
  90.  
  91.       if (bmhd->compression == cmpByteRun1) { /* compressed screen? */
  92.         rowbytes=linelen;
  93.  
  94.         while (rowbytes) { /* unpack until 1 scan-line complete */
  95.           n=*sourcebuf++; /* fetch block run marker */
  96.  
  97.           /* uncompressed block? copy n bytes verbatim */
  98.           if (n>=0) {
  99.             movmem(sourcebuf,destbuf,(unsigned int)++n); rowbytes-=n;
  100.             destbuf+=n; sourcebuf+=n;
  101.           }
  102.           else { /* compressed block? expand n duplicate bytes */
  103.             n=-n+1; rowbytes-=n;
  104.             setmem(destbuf,(unsigned int)n,(unsigned int)*sourcebuf++);
  105.             destbuf+=n;
  106.           }
  107.  
  108.         } /* finish unpacking line */
  109.       }
  110.       else { /* uncompressed? just copy */
  111.         movmem(sourcebuf,destbuf,(unsigned int)linelen);
  112.         sourcebuf+=linelen; destbuf+=linelen;
  113.       }
  114.     } /* finish interleaved planes, lines */
  115.     curloc += linelen;
  116.   }
  117. }
  118.